import wepy from 'wepy'
export default class extends wepy.mixin {
  data = {
    active: 0,
    pageIndex: '1',
    pageSize: '100',
    userId: '',
    // 全部订单
    allOrderList: [],
    // 代付款订单
    waitMoneyList: [],
    // 配送中订单
    sendList: [],
    // 已完成订单
    finishList: [],
    urls: wepy.$instance.globalData.baseURl,
    zwtu: '2020-05-07/202005071101471588820507809_e4v0z.png',
  }
  methods = {
    onChange(e) {
      console.log(e)
      if (e.detail.index == 0) {
        this.getOrder()
      } else if (e.detail.index == 1) {
        this.getWaitorder()
      } else if (e.detail.index == 2) {
        this.getSendOrder()
      } else if (e.detail.index == 3) {
        this.finishOrder()
      }
    },
    guangGuang() {
      console.log('去逛逛')
      wepy.switchTab({
        url: '../../pages/tabs/home',
      })
    },
    goOrder(id) {
      console.log(id)
      console.log('点击跳转到订单详情页面')
      wepy.navigateTo({
        url: '../../pages/Orderdatils/orderdatils?id=' + id,
      })
    },
    // 删除订单
    async deleteOrder(id, index) {
      console.log(id, index)
      var that = this
      const res = await wepy.showModal({
        title: '删除订单',
        content: '确定删除订单吗？',
      })
      if (res.confirm) {
        const { data: res } = await wepy.post('baseRest/deleteOrder', {
          id,
        })
        console.log(res)
        if (res.state != 1) {
          return wepy.noToast(res.message)
        }
        wepy.baseToast('删除成功')
      }
      setTimeout(function () {
        if (index == 0) {
          that.getOrder()
        } else if (index == 1) {
          that.getWaitorder()
        } else if (index == 2) {
          that.getSendOrder()
        } else if (index == 3) {
          that.finishOrder()
        }
      }, 300)
    },
  }
  computed = {
    // 判断页面是否有商品加入
    // 全部
    isAlllEmpty() {
      if (this.allOrderList.length <= 0) {
        return true
      }
      return false
    },
    // 代付款
    iswaitEmpty() {
      if (this.waitMoneyList.length <= 0) {
        return true
      }
      return false
    },
    // 已发货
    issendEmpty() {
      if (this.sendList.length <= 0) {
        return true
      }
      return false
    },
    // 已完成
    isfinishEmpty() {
      if (this.finishList.length <= 0) {
        return true
      }
      return false
    },
  }
  onLoad(e) {
    var i = Number(e.i)
    this.active = i
    let cid = wepy.getStorageSync('userId')
    this.userId = cid
    if (i == 0) {
      // 获取全部订单
      this.getOrder()
    } else if (i == 1) {
      // 获取代付款订单
      this.getWaitorder()
    } else if (i == 2) {
      // 获取配送中的订单
      this.getSendOrder()
    } else if (i == 3) {
      // 获取已完成订单
      this.finishOrder()
    }
  }
  // 获取全部订单
  async getOrder() {
    const { data: res } = await wepy.post('baseRest/getOrderList', {
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
      memberId: this.userId,
      orderState: '',
    })
    console.log('全部', res.data)
    if (res.state != 1) {
      return wepy.noToast('网络异常，请检查网络')
    }
    this.allOrderList = res.data
    this.$apply()
    // console.log('全部', res.data[0].goods_list)
  }
  // 获取代付款订单
  async getWaitorder() {
    const { data: res } = await wepy.post('baseRest/getOrderList', {
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
      memberId: this.userId,
      orderState: '1',
    })
    console.log('代付款', res)
    if (res.state != 1) {
      return wepy.noToast('网络异常，请检查网络')
    }
    this.waitMoneyList = res.data
    this.$apply()
  }
  // 获取配送中订单
  async getSendOrder() {
    const { data: res } = await wepy.post('baseRest/getOrderList', {
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
      memberId: this.userId,
      orderState: '3',
    })
    console.log('配送中', res)
    if (res.state != 1) {
      return wepy.noToast('网络异常，请检查网络')
    }
    this.sendList = res.data
    this.$apply()
  }
  // 已完成订单
  async finishOrder() {
    const { data: res } = await wepy.post('baseRest/getOrderList', {
      pageIndex: this.pageIndex,
      pageSize: this.pageSize,
      memberId: this.userId,
      orderState: '5',
    })
    console.log('已完成', res)
    if (res.state != 1) {
      return wepy.noToast('网络异常，请检查网络')
    }
    this.finishList = res.data
    this.$apply()
  }
}
